from typing import Any
from datetime import timedelta

from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm.session import Session
from common.deps import check_jwt_token, get_db
from core.response import response_code
from datetime import datetime
from setting.web import settings
from core.schemas.participant import ParticipantCreate,ParticipantDelete
from typing import Optional
import crud

router = APIRouter()


@router.post("/create", summary="添加出席人")
async def create_participant(new_participant:ParticipantCreate, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    crud.participant.create(db,new_participant)
    return response_code.resp_200(data={})

@router.post("/remove", summary="移除出席人")
async def remove_participant(delete_participant:ParticipantDelete, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.participant.remove(db,delete_participant)
    return response_code.resp_200(data=res)

@router.post("/sign", summary="手工签到")
async def sign_participant(delete_participant:ParticipantDelete, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.participant.remove(db,delete_participant)
    return response_code.resp_200(data=res)


@router.get("/listSchedule", summary="根据起始时间获取当前用户日程")
async def list_schedule( db: Session = Depends(get_db),startTime:str = datetime.now().strftime("%Y-%m-%d %H:%M"),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    converted_time = datetime.strptime(startTime, "%Y-%m-%d %H:%M")
    res = crud.participant.list_schedule(db,uid,startTime)
    if res != None:
        return response_code.resp_200(data=res)
        #return res.to_dict()
    return response_code.resp_500(message="获取失败")

@router.get("/listParticiPations", summary="获取所有参与数据")
async def list_participations(db: Session = Depends(get_db),startTime: Optional[str] = None,page:int = 1,pageSize:int  =20,payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    if startTime == None :
        res = crud.participant.list_participations(db,page,pageSize,uid)
    else:
        converted_time = datetime.strptime(startTime, "%Y-%m-%d %H:%M")
        res = crud.participant.list_participations_by_time(db,page,pageSize,uid,converted_time)
    return response_code.resp_200(data=res)
